using System;
using System.Collections.Generic;
using System.Text;
using SpoonCore2.Client;

namespace SpoonCore2.Service.Lookup
{
	public class LookupDatabaseMemory
	{
		private readonly SortedList<SpoonIdentifier, SortedList<SpoonIdentifier, byte[]>> _data = new SortedList<SpoonIdentifier, SortedList<SpoonIdentifier, byte[]>>();

		public bool Contrains(SpoonIdentifier typeId, SpoonIdentifier id)
		{
			if (_data.ContainsKey(typeId) && _data[typeId].ContainsKey(id)) return true;
			return false;
		}

		public byte[] Get(SpoonIdentifier typeId, SpoonIdentifier id)
		{
			if (_data.ContainsKey(typeId) && _data[typeId].ContainsKey(id)) return _data[typeId][id];
			return null;
		}

		public void Set(SpoonIdentifier typeId, SpoonIdentifier id, byte[] data)
		{
			if (!_data.ContainsKey(typeId)) _data.Add(typeId, new SortedList<SpoonIdentifier, byte[]>());
			if (_data[typeId].ContainsKey(id)) _data[typeId][id] = data;
			else _data[typeId].Add(id, data);
		}

		public byte[] this[SpoonIdentifier typeId, SpoonIdentifier id]
		{
			get { return Get(typeId, id); }
			set { Set(typeId, id, value); }
		}

		public void Remove(SpoonIdentifier typeId, SpoonIdentifier id)
		{
			if(_data.ContainsKey(typeId))
			{
				_data[typeId].Remove(id);
			}
		}

		public int Size
		{
			get
			{
				int sum = 0;
				foreach (SortedList<SpoonIdentifier, byte[]> list in _data.Values)
				{
					sum += list.Count;
				}
				return sum;
			}
		}
	}
}
